package com.spark.sniffer.plugin.plugins.define.trace;

import static net.bytebuddy.matcher.ElementMatchers.named;

import com.spark.sniffer.logging.api.Logger;
import com.spark.sniffer.logging.api.LoggerFactory;
import com.spark.sniffer.plugin.interceptor.InstanceMethodsInterceptPoint;
import com.spark.sniffer.plugin.interceptor.StaticMethodsInterceptPoint;
import com.spark.sniffer.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.spark.sniffer.plugin.match.ClassMatch;
import com.spark.sniffer.plugin.match.HierarchyMatch;
import com.spark.sniffer.plugin.plugins.interceptor.RedisTemplateInterceptor;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class RedisTemplatePlugin extends ClassInstanceMethodsEnhancePluginDefine {
    private static final Logger log = LoggerFactory.getLogger(RedisTemplatePlugin.class);

    @Override
    protected ClassMatch enhanceClass() {
        return HierarchyMatch.byHierarchyMatch(new String[] {"org.springframework.data.redis.core.RedisTemplate"});
    }

    @Override
    protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {new InstanceMethodsInterceptPoint() {
            // public ElementMatcher<MethodDescription> getMethodsMatcher() {
            // return named("delete").or(named("expire")).or(named("expireAt")).or(named("getExpire"))
            // .or(named("hasKey")).or(named("keys")).or(named("move")).or(named("persist")).or(named("randomKey"))
            // .or(named("rename")).or(named("renameIfAbsent"))
            // .or(named("restore").or(named("type")).or(named("exists")));

            // return ElementMatchers.anyOf(Arrays.asList("delete", "expire", "expireAt", "getExpire", "hasKey",
            // "keys", "move", "persist", "randomKey", "rename", "renameIfAbsent", "restore", "type", "exists"));

            @Override
            public ElementMatcher<MethodDescription> getMethodsMatcher() {
                return named("exist").or(named("hasKey")).or(named("delete")).or(named("expireAt"))
                    .or(named("getExpire")).or(named("keys")).or(named("move")).or(named("persist")).or(named("rename"))
                    .or(named("renameIfAbsent")).or(named("restore")).or(named("type")).or(named("expire"))
                    .or(named("del")).or(named("keys"));
            }

            @Override
            public Class<?> getMethodsInterceptor() {
                return RedisTemplateInterceptor.class;
            }

            @Override
            public boolean isOverrideArgs() {
                return false;
            }
        }};
    }

    @Override
    protected StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() {
        return null;
    }
}
